package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.behavior.entity.ApUnlikesBehavior;
import com.usian.behavior.mapper.ApUnlikesBehaviorMapper;
import com.usian.behavior.service.IApUnlikesBehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.common.threadlocal.UserThreadLocalUtils;
import com.usian.model.behavior.dtos.UnLikesBehaviorDto;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP不喜欢行为表 服务实现类
 * </p>
 *
 * @author usian
 * @since 2022-10-14
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior> implements IApUnlikesBehaviorService {

//    保存或更新 不喜欢行为
    @Override
    public void unLike(UnLikesBehaviorDto dto) {
//        参数校验
        if (dto==null){
            throw new LeadException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
//        判断用户是否登录
        Boolean isUser = false;
        //从线程中获取用户信息
        Integer userId = UserThreadLocalUtils.getUserId();
        if (userId!=null){
            isUser=true;
        }
//        查看用户或者设备号对当前文章进行过不喜欢
        QueryWrapper<ApUnlikesBehavior> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApUnlikesBehavior::getArticleId,dto.getArticleId());
        if (isUser){
            //用户不喜欢行为
            queryWrapper.lambda().eq(ApUnlikesBehavior::getApUserId,userId);
        }else {
            //设备号不喜欢行为
            queryWrapper.lambda().eq(ApUnlikesBehavior::getEquipmentId,dto.getEquipmentId());
        }
        ApUnlikesBehavior apUnlikesBehavior = this.getOne(queryWrapper);
//        如果没有不喜欢记录不喜欢行为
        if (apUnlikesBehavior==null){
            apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            if (isUser){
                apUnlikesBehavior.setApUserId(userId);
            }else {
                apUnlikesBehavior.setEquipmentId(dto.getEquipmentId());
            }
            apUnlikesBehavior.setType(dto.getType());
            apUnlikesBehavior.setCreatedTime(new Date());
            boolean b = this.save(apUnlikesBehavior);
            if (!b){
                throw new LeadException(AppHttpCodeEnum.SERVER_ERROR,"保存不喜欢行为失败");
            }
        }else {
//        如果不喜欢过，更新状态为取消，并记录修改的时间
            apUnlikesBehavior.setType(dto.getType());
            apUnlikesBehavior.setUpdateTime(new Date());
            boolean b = this.updateById(apUnlikesBehavior);
            if (!b){
                throw new LeadException(AppHttpCodeEnum.SERVER_ERROR,"更新不喜欢行为失败");
            }
        }
    }

//    查询文章是否被不喜欢
    @Override
    public ApUnlikesBehavior findUnLikeByArticleId(Integer userId, String equipmentId, Long articleId) {
//        判断是用户还是设备操作
        Boolean isUser = false;
        if (userId!=null && userId!=0){
            //用户登录
            isUser=true;
        }
//        封装查询条件
        QueryWrapper<ApUnlikesBehavior> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ApUnlikesBehavior::getArticleId,articleId);
        if (isUser){
            //用户操作
            queryWrapper.lambda().eq(ApUnlikesBehavior::getApUserId,userId);
        }else {
            //设备操作
            queryWrapper.lambda().eq(ApUnlikesBehavior::getEquipmentId,equipmentId);
        }
        ApUnlikesBehavior apUnlikesBehavior = this.getOne(queryWrapper);
        return apUnlikesBehavior;


    }
}
